﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Reflection;
using Pharmacy_Manage_System.Model;

namespace 静配辅助程序打印测试
{
    public class DBConnection
    {
        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="strCommand"></param>
        /// <param name="strConnect"></param>
        /// <returns></returns>
        public DataSet Getdata(string strCommand, string strConnect)
        {
            SqlConnection CNN = new SqlConnection(strConnect);
            SqlCommand CMD = new SqlCommand();
            DataSet dsData = new DataSet();
            try
            {
                CNN.Open();
                CMD.Connection = CNN;
                CMD.CommandText = strCommand;
                SqlDataAdapter dar = new SqlDataAdapter();
                dar.SelectCommand = CMD;
                dsData.Clear();
                dar.Fill(dsData, "Med");
            }
            catch
            {

            }
            finally
            {
                CNN.Close();
                CNN.Dispose();
            }
            return dsData;
        }

        /// <summary>
        /// 执行SQL语句
        /// </summary>
        /// <param name="strCommand"></param>
        /// <param name="strConnect"></param>
        public void ExceSql(string strCommand, String strConnect)
        {
            SqlConnection CNN = new SqlConnection(strConnect);
            SqlCommand CMD = new SqlCommand();
            try
            {
                CNN.Open();
                CMD.Connection = CNN;
                CMD.CommandText = strCommand;
                CMD.ExecuteNonQuery();
            }
            catch
            {

            }
            finally
            {
                CNN.Close();
                CNN.Dispose();
            }
        }

        /// <summary>
        /// 读取需连接的服务器
        /// </summary>
        /// <returns></returns>
        public DataSet ReadConfig()
        {
            try
            {
                string Apppath = Application.StartupPath;
                DataSet dsConfig = new DataSet();
                dsConfig.ReadXml(Apppath + "\\config.xml");
                return dsConfig;
            }
            catch (Exception error)
            {
                throw error;
            }
        }
        /// <summary> 
        /// DataSet装换为泛型集合 
        /// </summary> 
        /// <typeparam name="T"></typeparam> 
        /// <param name="p_DataSet">DataSet</param> 
        /// <param name="p_TableIndex">待转换数据表索引</param> 
        /// <returns></returns> 
        /// 2008-08-01 22:46 HPDV2806 
        public static List<LabelInfo> DataSetToIList<LabelInfo>(DataSet p_DataSet, int p_TableIndex)
        {
            if (p_DataSet == null || p_DataSet.Tables.Count < 0)
                return null;
            if (p_TableIndex > p_DataSet.Tables.Count - 1)
                return null;
            if (p_TableIndex < 0)
                p_TableIndex = 0;

            DataTable p_Data = p_DataSet.Tables[p_TableIndex];
            // 返回值初始化 
            List<LabelInfo> result = new List<LabelInfo>();
            for (int j = 0; j < p_Data.Rows.Count; j++)
            {
                LabelInfo _t = (LabelInfo)Activator.CreateInstance(typeof(LabelInfo));
                PropertyInfo[] propertys = _t.GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    for (int i = 0; i < p_Data.Columns.Count; i++)
                    {
                        // 属性与字段名称一致的进行赋值 
                        if (pi.Name.Equals(p_Data.Columns[i].ColumnName))
                        {
                            // 数据库NULL值单独处理 
                            if (p_Data.Rows[j][i] != DBNull.Value)
                                pi.SetValue(_t, p_Data.Rows[j][i], null);
                            else
                                pi.SetValue(_t, null, null);
                            break;
                        }
                    }
                }
                result.Add(_t);
            }
            return result;
        }


    }
}
